package com.tkjt.pms.impt.service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tkjt.pms.common.entity.TkIndexTree;
import com.tkjt.pms.impt.IIndexTreeImporterService;
import com.tkjt.pms.impt.repository.IndexTreeRepositories;
import com.tkjt.pms.modules.common.mapper.TkIndexTreeMapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@Transactional
public class IndexTreeImporter extends ServiceImpl<TkIndexTreeMapper, TkIndexTree> implements IIndexTreeImporterService {

	@Override
	public void doImporter(String filePath) {

		if (StringUtils.isEmpty(filePath)) {
			log.info("import by java repository");
			/**************************************
			 *   C\G层次关系要分开处理
			 */
			//处理 C(一线单位)层级关系
			this.importCRepository();
			//处理 G(养康本部)层级关系
			this.importGRepository();
		} else {
			// 从Excel文件导入，暂不用实理
		}

	}

	/***********************************************************************************************
	 * 处理 C(一线单位)层级关系
	 */
	private void importCRepository() {

		Integer nodeLevel = 1;
		String user = "admin";
		Date datetime = new Date();
		
		Set<String> parentCodes = new HashSet<String>();
		parentCodes.add(IndexTreeRepositories.INDEX_GROUP_C);
		do {

			Set<String> nextLevelIndexCodes = new HashSet<String>();
			for (String pCode : parentCodes) {
				List<String> indexCodes = IndexTreeRepositories.getSubCIndexs(pCode);
				nextLevelIndexCodes.addAll(indexCodes);
				for (String iCoede : indexCodes) {
					List<String> subCodes = IndexTreeRepositories.getSubCIndexs(iCoede);
					//如果是叶子节点
					if(subCodes.isEmpty()) {
						TkIndexTree entity = new TkIndexTree();
						entity.setIndexGroup("2"); // entity.setIndexGroup("C");
						if (pCode.equalsIgnoreCase("C")) {
							//entity.setNodeCodeParent("");
						} else {
							//entity.setNodeCodeParent(pCode);
						}
						entity.setNodeCode(iCoede);
						entity.setNodeCodeChild("");
						//entity.setNodeLevel(nodeLevel.toString());
						entity.setCreateBy(user);
						entity.setCreateDate(datetime);
						entity.setUpdateBy(user);
						entity.setUpdateDate(datetime);
						
						this.saveOrUpdate(entity);
						//处理叶子节点完成，返回
						continue;
					}
					for(String sCode : subCodes) {
						TkIndexTree entity = new TkIndexTree();
						entity.setIndexGroup("2"); // entity.setIndexGroup("C");
						if (pCode.equalsIgnoreCase("C")) {
							//entity.setNodeCodeParent("");
						} else {
							//entity.setNodeCodeParent(pCode);
						}
						entity.setNodeCode(iCoede);
						entity.setNodeCodeChild(sCode);
						//entity.setNodeLevel(nodeLevel.toString());
						entity.setCreateBy(user);
						entity.setCreateDate(datetime);
						entity.setUpdateBy(user);
						entity.setUpdateDate(datetime);
						
						this.saveOrUpdate(entity);
					}
				}
			}
			//处理下一级层级
			parentCodes = nextLevelIndexCodes;
			nodeLevel++;
			if(parentCodes.isEmpty()) {
				//所有节点处理完毕，退出
				break;
			}

		} while (true);
	}


	/***********************************************************************************************
	 * 处理 G(养康本部)层级关系
	 */
	private void importGRepository() {

		Integer nodeLevel = 1;
		String user = "admin";
		Date datetime = new Date();
		
		Set<String> parentCodes = new HashSet<String>();
		parentCodes.add(IndexTreeRepositories.INDEX_GROUP_G);
		do {

			Set<String> nextLevelIndexCodes = new HashSet<String>();
			for (String pCode : parentCodes) {
				List<String> indexCodes = IndexTreeRepositories.getSubGIndexs(pCode);
				nextLevelIndexCodes.addAll(indexCodes);
				for (String iCoede : indexCodes) {
					List<String> subCodes = IndexTreeRepositories.getSubGIndexs(iCoede);
					//如果是叶子节点
					if(subCodes.isEmpty()) {
						TkIndexTree entity = new TkIndexTree();
						entity.setIndexGroup("1"); // entity.setIndexGroup("C");
						if (pCode.equalsIgnoreCase("G")) {
							//entity.setNodeCodeParent("");
						} else {
							//entity.setNodeCodeParent(pCode);
						}
						entity.setNodeCode(iCoede);
						entity.setNodeCodeChild("");
						//entity.setNodeLevel(nodeLevel.toString());
						entity.setCreateBy(user);
						entity.setCreateDate(datetime);
						entity.setUpdateBy(user);
						entity.setUpdateDate(datetime);
						
						this.saveOrUpdate(entity);
						//处理叶子节点完成，返回
						continue;
					}
					for(String sCode : subCodes) {
						TkIndexTree entity = new TkIndexTree();
						entity.setIndexGroup("1"); // entity.setIndexGroup("C");
						if (pCode.equalsIgnoreCase("G")) {
							//entity.setNodeCodeParent("");
						} else {
							//entity.setNodeCodeParent(pCode);
						}
						entity.setNodeCode(iCoede);
						entity.setNodeCodeChild(sCode);
						//entity.setNodeLevel(nodeLevel.toString());
						entity.setCreateBy(user);
						entity.setCreateDate(datetime);
						entity.setUpdateBy(user);
						entity.setUpdateDate(datetime);
						
						this.saveOrUpdate(entity);
					}
				}
			}
			//处理下一级层级
			parentCodes = nextLevelIndexCodes;
			nodeLevel++;
			if(parentCodes.isEmpty()) {
				//所有节点处理完毕，退出
				break;
			}

		} while (true);
	}
}
